#include "service_camera.hpp"

namespace gimabl {
  
    ServiceCamera::ServiceCamera() 
    :m_exit(false),
    m_isRunning(false),
    counter(0),
    m_udpClient(nullptr)
    {
        
    }

    ServiceCamera::~ServiceCamera() {
        
    }

    int32_t ServiceCamera::UdpReadMsgHandler(uint8_t* buf,std::size_t size)
    {
        std::cout << "收到消息(hex): " << GetHexString(buf,size) << std::endl;
        auto msgPtr = std::unique_ptr<CameraPacket>(new CameraPacket(size));
        msgPtr->setAckData(buf,size);
        if(msgPtr->ParseData())
        {
            switch (msgPtr->getFlag())
            {
            case Response:
                {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                m_ackMsgMap.insert(std::make_pair(msgPtr->getMsgId(),std::move(msgPtr)));

                }
                break;
            case Status:
            case Report:
                {
                    std::lock_guard<std::mutex> lock(m_repMsgMutex);
                    m_repMsgQueue.push(std::move(msgPtr));
                }
                break;
            
            default:
                std::cout << "未知消息" << std::endl;
                break;
            }

        }else
        {
            std::cout << "解析失败" << std::endl;
        }
        return 0;
    }

    void ServiceCamera::ReportMsgHandler()
    {
        while (!m_exit)
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_repMsgQueue.empty())
            {
                std::lock_guard<std::mutex> lock(m_repMsgMutex);
                auto msgPtr=std::move(m_repMsgQueue.front());
                m_repMsgQueue.pop();

                std::cout << "report收到消息(hex): " << GetHexString(msgPtr->getDataBuf(),msgPtr->getDataLen()) << std::endl;
            }

            // if(!m_ackMsgMap.empty())
            // {
            //     std::lock_guard<std::mutex> lock(m_ackMsgMutex);
            //     auto it=m_ackMsgMap.begin();
            //     while (it!=m_ackMsgMap.end())
            //     {
            //         std::cout << "ack收到消息(hex): " << GetHexString(it->second->getDataBuf(),it->second->getDataLen()) << std::endl;
            //         it=m_ackMsgMap.erase(it);
            //     }
            // }

        }
    }

    int32_t ServiceCamera::InitServiceCamera(ThreadMode mode,std::string serverIp,int32_t serverPort,int32_t port,int32_t timeoutMs)
    {
        int32_t ret = -1;
        if(m_isRunning)
        {
            std::cout << "service camera is running" << std::endl;
            return ret;
        }
        try
        {
            m_udpClient = std::make_shared<UdpClient>(mode);
            ret=m_udpClient->udpInit(serverIp,serverPort,port);
            if(ret<0)
            {
                std::cout << "udp init failed" << std::endl;
                return ret;
            }

            auto handler = std::bind(&ServiceCamera::UdpReadMsgHandler,this,std::placeholders::_1,std::placeholders::_2);
            m_udpClient->udpRead(handler,timeoutMs);

            m_reportMsgHandlerThread = std::thread(&ServiceCamera::ReportMsgHandler,this);

            m_isRunning= true;
            ret=0;
        }
        catch(const std::exception& e)
        {
            std::cerr << e.what() << '\n';
            ret=-1;
        }
        
       
        return ret;
    }

    void ServiceCamera::StopServiceCamera()
    {
        m_udpClient->udpClose();
        m_isRunning=false;
        m_exit = true;
        if(m_reportMsgHandlerThread.joinable())
        {
            m_reportMsgHandlerThread.join();
        }
    }

    void ServiceCamera::sendMsg(uint8_t* buf,std::size_t size)
    {
        if(m_udpClient && m_isRunning)
        {
            m_udpClient->udpWrite(buf,size);
        }
    }

    /*-------------------- 基础功能 ---------------------------*/ 
    // 获取相机图像模式切换
    int32_t ServiceCamera::setDisplayMode(DisplayMode mode)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        uint8_t commond=mode;

        Payload[0] = commond ;
        Payload[1] = 0x00; // 保留位

        this->counter.fetch_add(1, std::memory_order_relaxed);

        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if (ret<0)
        {
            std::cout << "set payload failed" << std::endl;
            goto END;
        }
        // 这个指令模式，后续需要优化
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_3,FrameDstId::DstID_1,
                    FrameMsgId::MsgID_Mode,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }

        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }

        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(FrameMsgId::MsgID_Mode);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }
    // 精准倍率
    int32_t ServiceCamera::setAccurateZoob(float size)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x03;
        uint8_t Payload[PlayloadLen];
        uint32_t commond=size*10;
        Payload[0] = commond & 0xFF;
        Payload[1] = (commond >> 8) & 0xFF;
        Payload[2] = (commond >> 16) & 0xFF;

        this->counter.fetch_add(1, std::memory_order_relaxed);
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if (ret<0)
        {
            std::cout << "set payload failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,FrameDstId::DstID_2,
                        FrameMsgId::MsgID_Zoom,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }

        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(FrameMsgId::MsgID_Zoom);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }
    // Dynamic倍率
    int32_t ServiceCamera::setDynamicZoob(ZoomControlMode mode,ZoomAction action)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x03;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=mode;
        Payload[1]=action;
        Payload[2]=0x00;

        this->counter.fetch_add(1, std::memory_order_relaxed);
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if (ret<0)
        {
            std::cout << "set payload failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::COMMAND_SET_OPTICAL_ZOOM,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::COMMAND_SET_OPTICAL_ZOOM);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }
    // 设置对焦指令
    int32_t ServiceCamera::setFocus(ZoomControlMode mode,FocusAction action,Coordinate coordinate)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x0A;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=mode;
        Payload[1]=action;
        Payload[2]=coordinate.left_x & 0xff;
        Payload[3]=(coordinate.left_x >> 8) & 0xff;
        Payload[4]=coordinate.left_y & 0xff;
        Payload[5]=(coordinate.left_y >> 8) & 0xff;
        Payload[6]=coordinate.right_x & 0xff;
        Payload[7]=(coordinate.right_x >> 8) & 0xff;
        Payload[8]=coordinate.right_y & 0xff;
        Payload[9]=(coordinate.right_y >> 8) & 0xff;
        Payload[10]=0x00;

        this->counter.fetch_add(1, std::memory_order_relaxed);
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if (ret<0)
        {
            std::cout << "set payload failed" << std::endl;
            goto END;
        }

        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::COMMAND_SET_OPTICAL_FOCUS,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }

        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::COMMAND_SET_OPTICAL_FOCUS);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }

    /*--------------------------------拍照与录像-------------------------------------*/ 

    // 拍照录像模式切换
    int32_t ServiceCamera::switchCameraMode(CameraWorkMode mode)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        uint16_t commond=mode;

        Payload[0] = commond & 0xFF;
        Payload[1] = (commond >> 8) & 0xFF;

        this->counter.fetch_add(1, std::memory_order_relaxed);

        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if (ret<0)
        {
            std::cout << "set payload failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,FrameMsgId::MsgID_Picture_Mode,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }

        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(FrameMsgId::MsgID_Picture_Mode);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==Payload[0] && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
        }
        END:
        this->counter.fetch_sub(1, std::memory_order_relaxed);
        return ret;
    }
    // 拍照
    int32_t ServiceCamera::takePhoto(PhotoMode mode,CameraCommand command,std::string path,std::string name)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        int32_t i=0;
        uint8_t PlayloadLen = 0x36;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0] = mode;
        Payload[1]=command;

        if(path.length() > 20 || name.length() > 32)
        {
            std::cout << "path is too long" << std::endl;
            return -1;
        }
        
        for (uint8_t c : path) {
            Payload[2+i]=static_cast<uint8_t>(c);
            i++;
        }
        i=0;
        for (uint8_t c : name) {
            Payload[22+i]=static_cast<uint8_t>(c);
            i++;
        }
        this->counter.fetch_add(1, std::memory_order_relaxed);
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if (ret<0)
        {
            std::cout << "set payload failed" << std::endl;
            goto END;
        }

        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_1,FrameMsgId::MsgID_Picture,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }

        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(FrameMsgId::MsgID_Picture);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }
    // 录像
    int32_t ServiceCamera::recordVideo(VideoMode mode,VideoControlCommand command,std::string path,std::string name)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        int32_t i=0;
        uint8_t PlayloadLen = 0x36;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0] = mode;
        Payload[1]=command;

        if(path.length() > 20 || name.length() > 32)
        {
            std::cout << "path is too long" << std::endl;
            return -1;
        }

        for (uint8_t c : path) {
            Payload[2+i]=static_cast<uint8_t>(c);
            i++;
        }
        i=0;
        for (uint8_t c : name) {
            Payload[22+i]=static_cast<uint8_t>(c);
            i++;
        }

        this->counter.fetch_add(1, std::memory_order_relaxed);
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if (ret<0)
        {
            std::cout << "set payload failed" << std::endl;
            goto END;
        }
        packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,FrameMsgId::MsgID_Record,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }

        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(FrameMsgId::MsgID_Record);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }

        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }


    /*--------------------------相机设置--------------------------------*/ 

    // 录像分辨率选择
    int32_t ServiceCamera::setVisibleRecodeResolution(VideoResolution command,ResolutionType type)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=command;
        Payload[1]=type;

        this->counter.fetch_add(1, std::memory_order_relaxed);
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if (ret<0)
        {
            std::cout << "set payload failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::COMMAND_SET_VISIBLE_RECORD,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::COMMAND_SET_VISIBLE_RECORD);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }

    // 拍照分辨率选择
    int32_t ServiceCamera::setVisiblePitureResolution( PhotoResolutionSetting command,ResolutionType type)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=command;
        Payload[1]=type;

        this->counter.fetch_add(1, std::memory_order_relaxed);
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if (ret<0)
        {
            std::cout << "set payload failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::COMMAND_SET_VISIBLE_PITURE,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::COMMAND_SET_VISIBLE_PITURE);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }

    // 预览流分辨率选择
    int32_t ServiceCamera::setVisibleVideoResolution(VideoOutputMode mode,VideoResolutionFpsSetting command)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=mode;
        Payload[1]=command;

        this->counter.fetch_add(1, std::memory_order_relaxed);
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if (ret<0)
        {
            std::cout << "set payload failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::COMMAND_SET_VISIBLE_VIDEO_OUTPUT_FORMAT,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }

        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::COMMAND_SET_VISIBLE_VIDEO_OUTPUT_FORMAT);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }


    // 视频输出编码格式 
    int32_t ServiceCamera::setVideoEncodingFormat(VideoEncodingFormat format)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=format;
        Payload[1]=0x00;

        this->counter.fetch_add(1, std::memory_order_relaxed);
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if (ret<0)
        {
            std::cout << "set payload failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::COMMAND_SET_VIDEO_OUTPUT_FARMAT,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::COMMAND_SET_VIDEO_OUTPUT_FARMAT);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;

    }
    // 拍照类型选择，连续拍，延时拍等
    int32_t ServiceCamera::setTakePhotoType(TakePhotoType command,int32_t time,int32_t num)
    {
        int32_t ret=-1;
        int32_t g_num=0; // 消息计数
        uint8_t PlayloadLen = 0x04;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=command;
        Payload[1]=time;
        Payload[2]=num;
        Payload[3]=0x00;

        this->counter.fetch_add(1, std::memory_order_relaxed);

        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::COMMAND_GET_VISIBLE_EXPOSURE,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(g_num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::COMMAND_GET_VISIBLE_EXPOSURE);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            g_num++;
        }
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }

    // 照片存储设置
    int32_t ServiceCamera::setPhotoSaveMode(PhotoFlags command)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0] = command;
        Payload[1] = 0x00;

        this->counter.fetch_add(1, std::memory_order_relaxed);
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::PhotoStorageSettingCommand,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::PhotoStorageSettingCommand);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }

    // 视频存储设置
    int32_t ServiceCamera::setVideoSaveMode( PhotoFlags command)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0] = command;
        Payload[1] = 0x00;

        this->counter.fetch_add(1, std::memory_order_relaxed);
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::VideoStorageSettingCommand,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }

        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::VideoStorageSettingCommand);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }

    // 自定义照片和视频存储文件夹指令
    int32_t ServiceCamera::setVideoAndPhotoSavePath( std::string path)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        int32_t i=0;
        uint8_t PlayloadLen = 0x2a;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);

        if(path.length() > 40)
        {
            std::cout << "path is too long" << std::endl;
            return ret;
        }
        for (uint8_t c : path) {
            Payload[i]=static_cast<uint8_t>(c);
            i++;
        };
        this->counter.fetch_add(1, std::memory_order_relaxed);
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::CustomPhotoAndVideoStorageFolder,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::CustomPhotoAndVideoStorageFolder);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
  
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }

    // 获取自定义照片和视频存储文件夹路径
    int32_t ServiceCamera::getVideoAndPhotoSavePath()
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);

        this->counter.fetch_add(1, std::memory_order_relaxed);
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::GetCustomPhotoAndVideoStorageFolder,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }

        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::GetCustomPhotoAndVideoStorageFolder);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }

    // 恢复出厂设置
    int32_t ServiceCamera::factoryReset(resetCommand param, ReservedCode reserved)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=param;
        Payload[1]=reserved;
        this->counter.fetch_add(1, std::memory_order_relaxed);    
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,FrameMsgId::MsgID_Restore,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(FrameMsgId::MsgID_Restore);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }

    // 格式化SD卡
    int32_t ServiceCamera::formatSDCard(FormatSDCardCommand param, ReservedCode reserved)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=param;
        Payload[1]=reserved;

        this->counter.fetch_add(1, std::memory_order_relaxed);
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,FrameMsgId::MsgID_FormatSDCard,0);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(FrameMsgId::MsgID_FormatSDCard);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }

    /*------------------------ 其他功能 -------------------------------*/ 

    // 获取相机可见参数
    int32_t ServiceCamera::getAllVisibleParams(CameraType command)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=0x01;
        Payload[1]=command;

        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::COMMAND_GET_VISIBLE_PARAMS,0);
        
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::COMMAND_GET_VISIBLE_PARAMS);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }
    //  设置保存图片格式
    int32_t  ServiceCamera::setVisiblePitureSaveFormat(ImageSaveFormat command)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=command;
        Payload[1]=0x00;

        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::COMMAND_SET_VISIBLE_PITURE_FORMAT,0);
        
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::COMMAND_SET_VISIBLE_PITURE_FORMAT);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }
    // 设置录播视频格式
    int32_t  ServiceCamera::setVisibleRecodeSaveFormat(VideoSaveFormat command)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=command;
        Payload[1]=0x00;

        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::COMMAND_SET_VISIBLE_RECORD_FORMAT,0);
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::COMMAND_SET_VISIBLE_RECORD_FORMAT);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;

    }
    // 设置预览流分辨率码率
    int32_t  ServiceCamera::setVisibleVideoResolutionBitrate(VideoResolutionBitrate command)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=command;
        Payload[1]=0x00;

        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::COMMAND_SET_VISIBLE_VIDEO_STREAM_FLOW,0);
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::COMMAND_SET_VISIBLE_VIDEO_STREAM_FLOW);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }

    // 设置开启相机数据上报
    int32_t  ServiceCamera::setCameraReproSwitch(ReproSwitchMode command)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=command;
        Payload[1]=0x00;
        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,0xfe01,0);

        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(0xfe01);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }
    
    // 获取相机版本号
    int32_t  ServiceCamera::getCameraVersion(DevicesType type)
    {
        int32_t ret=-1;
        int32_t num=0; // 消息计数
        uint8_t PlayloadLen = 0x02;
        uint8_t Payload[PlayloadLen];
        memset(Payload,0,PlayloadLen);
        Payload[0]=type;
        Payload[1]=0x00;

        CameraPacket packet(FRAME_FIXED_LEN+PlayloadLen);
        ret=packet.setPayload(Payload, PlayloadLen);
        if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        ret=packet.InitPacket(FRAME_HEADER,FrameType::Message,FrameFlag::Request,FrameSrcId::SrcID_1,
            FrameDstId::DstID_2,VisibleCameraCommand::COMMAND_GET_VERSION,0);
         if(ret<0)
        {
            std::cout << "init packet failed" << std::endl;
            goto END;
        }
        if(m_udpClient &&!m_udpClient->isStop())
        {
            ret=m_udpClient->udpWrite(packet.getDataBuf(), packet.getDataLen());
            if(ret<0)
            {
                std::cout << "udp write failed" << std::endl;
                goto END;
            }
        }
        while (m_isRunning)
        {
            if(num >10)
            {
                std::cout << "消息超时" << std::endl;
                ret=-1;
                break;
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            if(!m_ackMsgMap.empty())
            {
                std::lock_guard<std::mutex> lock(m_ackMsgMutex);
                auto it=m_ackMsgMap.find(VisibleCameraCommand::COMMAND_GET_VERSION);
                if(it!=m_ackMsgMap.end())
                {
                    if(it->second->getPayloadData()[0]==0x00 && 
                    it->second->getPayloadData()[1]==0x00)
                    {
                        ret=0;
                        std::cout << "控制成功" << std::endl;
                    }else
                    {
                        ret=-1;
                        std::cout << "控制失败" << std::endl;
                    }
                    m_ackMsgMap.erase(it->first);
                    break;
                }
            }
            num++;
        }
        END:
            this->counter.fetch_sub(1, std::memory_order_relaxed);
            return ret;
    }


}

